<?php


namespace app\api\controller;




use app\common\controller\Api;
use app\common\model\MedicalInfo;
use app\common\model\UserCards;
use think\Model;

class Mine extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 我的
     */
    public function mineInfoList()
    {
        $params =$this->request->param();
        $data=(new MedicalInfo())
            ->where(['user_id'=>$this->auth->id])
            ->paginate();
        $this->success('',$data);
    }


    public function mineInfoDetail()
    {
        $params =$this->request->param();
        $data=(new MedicalInfo())
            ->where(['user_id'=>$this->auth->id,'id'=>$params['id']])
            ->find();
        $this->success('',$data);
    }

    public function deleteinfo()
    {
        $params =$this->request->param();
        $res=(new MedicalInfo())
            ->where(['user_id'=>$this->auth->id,'id'=>$params['id']])
            ->delete();
        if($res){
            $this->success('');
        }else{
            $this->error('删除失败');
        }
    }

    public function saveInfo()
    {

       $params= $this->request->post('');
        $saves=['medical_username'=>$params['medical_username'],'medical_gender'=>$params['medical_gender'],
        'medical_cardno'=>$params['medical_cardno'],'medical_birth'=>$params['medical_birth']
        ];
        if(empty($saves['medical_birth'])) {
            $this->error('请选择生日');
        }
        if(!empty($saves['medical_cardno'])) {
        if(!is_idcard($saves['medical_cardno'])){
            $this->error('身份证格式错误');
        }
        }


        $res=false;
        if(!empty($params['info_id'])){
           $msg ='更新失败';
           $msg1='更新成功';
           $exist= (new MedicalInfo())->where(['id'=>$params['info_id'],'user_id'=>$this->auth->id])->find();
           if(!$exist){
               $msg='未查询到该就诊人信息';
           }else{
               $saves['id']=$params['info_id'];
              $res = (new  MedicalInfo())->isUpdate(true)->save($saves);
           }
        }else{
            $msg ='新增失败';
            $msg1='新增成功';
            $saves['user_id']=$this->auth->id;
            $res = (new MedicalInfo())->save($saves);
        }
        if($res){
            $this->success($msg1);
        }else{
            $this->error($msg);
        }

    }


    public function checkInfo()
    {
        $param =$this->request->param('');
        if(empty($param['card_id'])){
            $this->error('请选择权益卡');
        }
        $card = (new UserCards())->where(['user_id'=>$this->auth->id,'id'=>$param['card_id']])->find();
        if(!$card){
            //     \Exception('不存在该权益');
            $data['msg']='不存在该权益';
            $this->error('不存在该权益');
        }
        if($card['rights_endtime']<=time()){
            $this->error('权益已过期');
        }
        if($card['can_uses']<1){
            $data['msg']='权益使用完毕';
            $this->error('权益使用完毕');
        }
        if($card['can_uses']<1){
            $data['msg']='权益使用完毕';
            $this->error('权益使用完毕');
        }
        if($card['is_show']!=1&&$card['status']==1){
            $this->error('退款中不可预约');
        }
        if($card['status']==2){
            $this->error('权益使用完毕');
        }
        if($card['status']==3){
            $this->error('已退款不可预约');
        }
        $this->success('');
    }
}